package com.jiazhong.basic.shuxin.thread.b;

import lombok.SneakyThrows;

import java.util.Stack;

public class Broker {
    private Stack<Goods> stack = new Stack<>();
    private final int max = 10;

    // 进货
    @SneakyThrows
    public synchronized void purchase(Goods goods) {
        if (stack.size() == max) {
            System.out.println("仓库满了 不要在进货了");
            // 仓库满了 不要在进货了
            wait();
        }
        stack.push(goods);
        notify();
    }

    @SneakyThrows
    public synchronized Goods sell() {
        if (stack.empty()) {
            System.out.println("缺货中，请稍等");
            wait();
        }
        Goods pop = stack.pop();
        notify();
        return pop;
    }
}
